#!/usr/bin/env sh
#set -xe
pid=0

stop_handler() {
  if [ $pid -ne 0 ]; then
    kill -SIGTERM "$pid"
    wait "$pid"
  fi
  exit 0;
}

trap 'kill ${!}; stop_handler' TERM INT QUIT KILL STOP

# run application
/app/"${BINARY_NAME}" http -p "${HTTP_PORT}" &
pid="$!"
sleep 1
pid_exist=$(ps aux | awk '{print $1}'| grep -w $pid)
if [ ! "$pid_exist" ]; then
  echo "process not running"
  exit 1
fi
# wait forever
while true
do
  tail -f /dev/null & wait ${!}
done
